<template>
	<view class="mainpadding2" v-if="pageShow">
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">工作站名稱</view>
			<input class="main_size bold hei_text" type="text" placeholder="請輸入工作站名稱" style="text-align: right;"
				v-model="name">
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">同步製作</view>
			<input class="main_size bold hei_text" type="number" placeholder="請輸入" style="text-align: right;"
				v-model="tbzz">
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">轉檔</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="zd_status" activeColor="#feb800"></u-switch>
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">二維碼完成開關</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="outgoods_status" activeColor="#feb800"></u-switch>
		</view>
		<view class="mainpadding_top ffffff radius flexbetween" @click="goSelectDa(1)">
			<view class="flexleft">
				<view class="titletext bold hei_text margin_right1" style="width: 160rpx;">收據打印機</view>
				<view class="jiebang" @click.stop="sj_id='0';sj_name=''" v-if="sj_name"></view>
			</view>
			<view class="flexleft">
				<input class="main_size bold hei_text" type="text" disabled placeholder="請選擇收據打印機"
					style="text-align: right;" v-model='sj_name'>
				<u-icon name="arrow-right" color="#999" size="14"></u-icon>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius flexbetween" @click="goSelectDa(2)">
			<view class="flexleft">
				<view class="titletext bold hei_text margin_right1" style="width: 160rpx;">標籤印表機</view>
				<view class="jiebang" @click.stop="bq_id='0';bq_name=''" v-if="bq_name"></view>
			</view>
			<view class="flexleft">
				<input class="main_size bold hei_text" type="text" disabled placeholder="請選擇標籤印表機"
					style="text-align: right;" v-model='bq_name'>
				<u-icon name="arrow-right" color="#999" size="14"></u-icon>
			</view>
		</view>
		<view class="mainpadding posibottom" style="bottom: 0;">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				name: "", //工作站名称
				sj_id: "", //收据打印機id
				sj_name: "", //收据打印機名称
				bq_id: "", //标签打印機id
				bq_name: "", //标签打印機名称
				type: "", //类型:1=結帳區,2=工作站
				id: "", //工作站id
				pageShow: false,
				issubmit: false,
				outgoods_status: false,
				zd_status:false,
				tbzz:"",
			}
		},
		onLoad(options) {
			uni.$on("selectInfo", (data) => {
				if (data.type == 1) {
					this.sj_id = data.id
					this.sj_name = data.name
				} else {
					this.bq_id = data.id
					this.bq_name = data.name
				}
			})
			if (options.id) {
				this.id = options.id
				uni.setNavigationBarTitle({
					title: "編輯工作站"
				})
				this.getWorkInfo()
			} else {
				this.pageShow = true
			}
		},
		onUnload() {
			uni.$off("selectInfo")
		},
		methods: {
			async getWorkInfo() {
				let _this = this
				let res = await this.http.request("/api/shop/Workdeail", "POST", {
					shop_id: _this.shop_id,
					id: this.id
				})
				if (res.code == 1) {
					this.name = res.data.name
					this.sj_id = res.data.sj_id
					this.sj_name = res.data.sj_data ? res.data.sj_data.name : ""
					this.bq_id = res.data.bq_id
					this.bq_name = res.data.bq_data ? res.data.bq_data.name : ""
					this.outgoods_status = res.data.outgoods_status == 1 ? true : false
					this.zd_status = res.data.zd_status == 1 ? true : false
					this.tbzz = res.data.tbzz
					this.pageShow = true
				} else if (res.code == 0) {
					this.http.toast(res.msg);
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			goSelectDa(type) { //選擇打印機
				uni.navigateTo({
					url: "/pages/workbench/dayinjgl?selectType=1&type=" + type
				})
			},
			async submit() { //確認
				let _this = this
				let url = "",
					data = "",
					text = "";
				if (!this.name) {
					this.http.toast("請輸入工作站名稱！")
					return false
				}
				// if (!this.sj_id) {
				// 	this.http.toast("請選擇收据打印機！")
				// 	return false
				// }
				// if (!this.bq_id) {
				// 	this.http.toast("請選擇标签打印機！")
				// 	return false
				// }
				if (this.issubmit) return
				this.issubmit = true
				if (!this.id) {
					url = "/api/shop/addWork"
					data = {
						shop_id: _this.shop_id,
						name: _this.name, //工作站名称
						sj_id: _this.sj_id, //标签打印機id
						bq_id: _this.bq_id, //收据打印機id
						type: 1, //类型:0=結帳區,1=工作站
						outgoods_status: this.outgoods_status ? 1 : 0,
						zd_status: this.zd_status ? 1 : 0,
						tbzz:this.tbzz,
					}
					text = "添加成功"
				} else {
					url = "/api/shop/editWork"
					data = {
						shop_id: _this.shop_id,
						name: _this.name, //工作站名称
						sj_id: _this.sj_id, //标签打印機id
						bq_id: _this.bq_id, //收据打印機id
						type: 1, //类型:0=結帳區,1=工作站
						id: _this.id, //工作站id
						outgoods_status: this.outgoods_status ? 1 : 0,
						zd_status: this.zd_status ? 1 : 0,
						tbzz:this.tbzz,
					}
					text = "修改成功"
				}
				let res = await this.http.request(url, 'POST', data)
				if (res.code == 1) {
					this.http.toast(text)
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>